package com.yc.cloud.openai.portal.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yc.cloud.common.basic.dto.BaseQueryParam;
import com.yc.cloud.common.oss.service.OssFileService;
import com.yc.cloud.openai.entity.VideoRecord;
import com.yc.cloud.openai.mapper.VideoRecordMapper;
import com.yc.cloud.openai.portal.dto.video.VideoRecordListParam;
import com.yc.cloud.openai.portal.service.VideoRecordService;
import com.yc.cloud.openai.vo.VideoRecordVo;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import lombok.val;

/**
 * <p>
 * AI视频记录 服务实现类
 * </p>
 *
 * @author Yancy
 * @since 2025-02-13
 */
@Service
public class VideoRecordServiceImpl extends ServiceImpl<VideoRecordMapper, VideoRecord> implements VideoRecordService {

    private final OssFileService ossFileService;

    public VideoRecordServiceImpl(OssFileService ossFileService) {
        this.ossFileService = ossFileService;
    }

    @Override
    public IPage<VideoRecordVo> list(VideoRecordListParam param, BaseQueryParam queryParam) {
        val query = new QueryWrapper<VideoRecord>();
        IPage<VideoRecord> page;
        val clientIds = StrUtil.split(param.getClientId(), ",");
        if (clientIds.size() == 1) {
            query.eq(VideoRecord.CLIENT_ID, clientIds.get(0));
        } else {
            query.in(VideoRecord.CLIENT_ID, clientIds);
        }
        if (CollUtil.isNotEmpty(param.getStatusList())) {
            query.in(VideoRecord.STATUS, param.getStatusList());
        }
        // 排序
        if (StrUtil.isNotEmpty(queryParam.getSortBy())) {
            if (BaseQueryParam.ORDER_DESC.equals(queryParam.getOrder())) {
                query.orderByDesc(queryParam.getSortBy());
            } else if (BaseQueryParam.ORDER_ASC.equals(queryParam.getOrder())) {
                query.orderByAsc(queryParam.getSortBy());
            }
        }
        // 模糊查询
        // if (StrUtil.isNotEmpty(queryParam.getKeyword())) {
        // query.and(queryWrapper -> {

        // });
        // }
        // 分页
        if (queryParam.getPage()) {
            page = new Page<>(queryParam.getPageNum(), queryParam.getPageSize(), true);
        } else {
            page = new Page<>(queryParam.getPageNum(), baseMapper.selectCount(query), true);
        }
        return baseMapper.selectPage(page, query).convert(this::convertVo);
    }

    @Override
    public VideoRecord create(VideoRecord param) {
        val videorecord = new VideoRecord();
        BeanUtils.copyProperties(param, videorecord);
        val saveFlag = save(videorecord);
        return saveFlag ? videorecord : null;
    }

    @Override
    public VideoRecordVo get(Long id) {
        return convertVo(getById(id));
    }

    @Override
    public boolean delete(Long id) {
        return removeById(id);
    }

    @Override
    public VideoRecord update(Long id, VideoRecord param) {
        val videorecord = new VideoRecord();
        BeanUtils.copyProperties(param, videorecord);
        videorecord.setId(id);
        val updateFlag = updateById(videorecord);
        return updateFlag ? videorecord : null;
    }

    @Override
    public VideoRecordVo convertVo(VideoRecord videorecord) {
        if (videorecord == null) {
            return null;
        }
        val vo = new VideoRecordVo();
        BeanUtils.copyProperties(videorecord, vo);
        // 同时去查询此次任务生成的图片
        val ossFileList = ossFileService.listByParentFileId(videorecord.getUuid());
        // 转换成VO对象
        vo.setVideos(ossFileService.convertVo(ossFileList));
        return vo;
    }

    @Override
    public List<VideoRecordVo> convertVo(List<VideoRecord> VideoRecordList) {
        if (CollUtil.isNotEmpty(VideoRecordList)) {
            return VideoRecordList.stream().map(this::convertVo).collect(Collectors.toList());
        }
        return new ArrayList<>();
    }

    @Override
    public List<VideoRecord> listByStatus(List<String> statusList) {
        return baseMapper.selectList(new QueryWrapper<VideoRecord>().in(VideoRecord.STATUS, statusList));
    }
}
